// Copyright 2008 Thiago H. de Paula Figueiredo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package br.com.arsmachina.module.factory.impl;

import java.io.Serializable;

import org.hibernate.SessionFactory;

import br.com.arsmachina.dao.DAO;
import br.com.arsmachina.dao.hibernate.ConcreteDAOImpl;
import br.com.arsmachina.module.factory.DAOFactory;

/**
 * {@link DefaultDAOFactory} implementation using Hibernate.
 * 
 * @author Thiago H. de Paula Figueiredo
 */
public class HibernateDAOFactory implements DAOFactory {

	final private SessionFactory sessionFactory;

	/**
	 * Single constructor of this class.
	 * 
	 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
	 */
	public HibernateDAOFactory(SessionFactory sessionFactory) {

		if (sessionFactory == null) {
			throw new IllegalArgumentException("Parameter sessionFactory cannot be null");
		}

		this.sessionFactory = sessionFactory;

	}

	public <T> DAO<T, ?> build(Class<T> entityClass) {
		
		DAO<T, ?> dao = null;
		
		if (sessionFactory.getClassMetadata(entityClass) != null) {
			dao = new ConcreteDAOImpl<T, Serializable>(entityClass, sessionFactory);
		}
		
		return dao;
		
	}

}
